package cn.ibizlab.odoo.core.odoo_product.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproduct_category;
import cn.ibizlab.odoo.core.odoo_product.domain.Product_category;
import cn.ibizlab.odoo.core.odoo_product.filter.Product_categorySearchContext;
import cn.ibizlab.odoo.core.odoo_product.service.IProduct_categoryService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[产品种类] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Product_categoryServiceImpl implements IProduct_categoryService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproduct_categoryClientService product_categoryClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproduct_category clientModel = product_categoryClientService.createModel();
        clientModel.setId(id);
        product_categoryClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Product_category product_category) {
        boolean bOk = false ;
        Iproduct_category clientModel = this.convert2Model(product_category,null);
        product_categoryClientService.create(clientModel) ;
        this.convert2Domain(clientModel,product_category);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Product_category> product_categorys, int batchSize) {

        //product_categoryClientService.updateBatch(product_categorys,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //product_categoryClientService.removeBatch(product_categorys) ;

    }

    @Override
    public boolean update(Product_category product_category) {
        boolean bOk = false ;
        Iproduct_category clientModel = this.convert2Model(product_category,null);
        product_categoryClientService.update(clientModel) ;
        this.convert2Domain(clientModel,product_category) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Product_category> product_categorys, int batchSize) {

                        //product_categoryClientService.createBatch(product_categorys,batchSize) ;

    }

    @Override
    public Product_category get(Integer id) {
        Iproduct_category clientModel = product_categoryClientService.createModel();
        clientModel.setId(id);
        product_categoryClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Product_category get(Product_category product_category) {
        Product_category retproduct_category = this.get(product_category.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproduct_category.getClass(), product_category.getClass(), false);
        copier.copy(retproduct_category, product_category, null);
        return product_category ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Product_category> searchDefault(Product_categorySearchContext context) {
        List<Product_category> list = new ArrayList<Product_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_category> clientModelList = product_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_category>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据产品种类查询
     */
    @Override
    public Page<Product_category> selectByOdoo_parent(Integer id){
        Product_categorySearchContext context = new Product_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_category> list = new ArrayList<Product_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_category> clientModelList = product_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据下架策略查询
     */
    @Override
    public Page<Product_category> selectByOdoo_removal_strategy(Integer id){
        Product_categorySearchContext context = new Product_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_category> list = new ArrayList<Product_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_category> clientModelList = product_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_category> selectByOdoo_create(Integer id){
        Product_categorySearchContext context = new Product_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_category> list = new ArrayList<Product_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_category> clientModelList = product_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Product_category> selectByOdoo_write(Integer id){
        Product_categorySearchContext context = new Product_categorySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Product_category> list = new ArrayList<Product_category>();
        Page<cn.ibizlab.odoo.core.client.model.Iproduct_category> clientModelList = product_categoryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproduct_category clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Product_category>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproduct_category convert2Model(Product_category domain , Iproduct_category model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = product_categoryClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getProperty_stock_valuation_account_idDirtyFlag())
            model.setProperty_stock_valuation_account_id(domain.getProperty_stock_valuation_account_id());
         if(domain.getChild_idDirtyFlag())
            model.setChild_id(domain.getChild_id());
         if(domain.getProduct_countDirtyFlag())
            model.setProduct_count(domain.getProduct_count());
         if(domain.getRoute_idsDirtyFlag())
            model.setRoute_ids(domain.getRoute_ids());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getProperty_valuationDirtyFlag())
            model.setProperty_valuation(domain.getProperty_valuation());
         if(domain.getParent_pathDirtyFlag())
            model.setParent_path(domain.getParent_path());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getComplete_nameDirtyFlag())
            model.setComplete_name(domain.getComplete_name());
         if(domain.getProperty_account_creditor_price_difference_categDirtyFlag())
            model.setProperty_account_creditor_price_difference_categ(domain.getProperty_account_creditor_price_difference_categ());
         if(domain.getProperty_stock_account_output_categ_idDirtyFlag())
            model.setProperty_stock_account_output_categ_id(domain.getProperty_stock_account_output_categ_id());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getTotal_route_idsDirtyFlag())
            model.setTotal_route_ids(domain.getTotal_route_ids());
         if(domain.getProperty_cost_methodDirtyFlag())
            model.setProperty_cost_method(domain.getProperty_cost_method());
         if(domain.getProperty_account_income_categ_idDirtyFlag())
            model.setProperty_account_income_categ_id(domain.getProperty_account_income_categ_id());
         if(domain.getProperty_account_expense_categ_idDirtyFlag())
            model.setProperty_account_expense_categ_id(domain.getProperty_account_expense_categ_id());
         if(domain.getProperty_stock_journalDirtyFlag())
            model.setProperty_stock_journal(domain.getProperty_stock_journal());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getProperty_stock_account_input_categ_idDirtyFlag())
            model.setProperty_stock_account_input_categ_id(domain.getProperty_stock_account_input_categ_id());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getParent_id_textDirtyFlag())
            model.setParent_id_text(domain.getParent_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getRemoval_strategy_id_textDirtyFlag())
            model.setRemoval_strategy_id_text(domain.getRemoval_strategy_id_text());
         if(domain.getParent_idDirtyFlag())
            model.setParent_id(domain.getParent_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getRemoval_strategy_idDirtyFlag())
            model.setRemoval_strategy_id(domain.getRemoval_strategy_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Product_category convert2Domain( Iproduct_category model ,Product_category domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Product_category();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getProperty_stock_valuation_account_idDirtyFlag())
            domain.setProperty_stock_valuation_account_id(model.getProperty_stock_valuation_account_id());
        if(model.getChild_idDirtyFlag())
            domain.setChild_id(model.getChild_id());
        if(model.getProduct_countDirtyFlag())
            domain.setProduct_count(model.getProduct_count());
        if(model.getRoute_idsDirtyFlag())
            domain.setRoute_ids(model.getRoute_ids());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getProperty_valuationDirtyFlag())
            domain.setProperty_valuation(model.getProperty_valuation());
        if(model.getParent_pathDirtyFlag())
            domain.setParent_path(model.getParent_path());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getComplete_nameDirtyFlag())
            domain.setComplete_name(model.getComplete_name());
        if(model.getProperty_account_creditor_price_difference_categDirtyFlag())
            domain.setProperty_account_creditor_price_difference_categ(model.getProperty_account_creditor_price_difference_categ());
        if(model.getProperty_stock_account_output_categ_idDirtyFlag())
            domain.setProperty_stock_account_output_categ_id(model.getProperty_stock_account_output_categ_id());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getTotal_route_idsDirtyFlag())
            domain.setTotal_route_ids(model.getTotal_route_ids());
        if(model.getProperty_cost_methodDirtyFlag())
            domain.setProperty_cost_method(model.getProperty_cost_method());
        if(model.getProperty_account_income_categ_idDirtyFlag())
            domain.setProperty_account_income_categ_id(model.getProperty_account_income_categ_id());
        if(model.getProperty_account_expense_categ_idDirtyFlag())
            domain.setProperty_account_expense_categ_id(model.getProperty_account_expense_categ_id());
        if(model.getProperty_stock_journalDirtyFlag())
            domain.setProperty_stock_journal(model.getProperty_stock_journal());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getProperty_stock_account_input_categ_idDirtyFlag())
            domain.setProperty_stock_account_input_categ_id(model.getProperty_stock_account_input_categ_id());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getParent_id_textDirtyFlag())
            domain.setParent_id_text(model.getParent_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getRemoval_strategy_id_textDirtyFlag())
            domain.setRemoval_strategy_id_text(model.getRemoval_strategy_id_text());
        if(model.getParent_idDirtyFlag())
            domain.setParent_id(model.getParent_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getRemoval_strategy_idDirtyFlag())
            domain.setRemoval_strategy_id(model.getRemoval_strategy_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
